package com.furnitur.modules.admin.controller.merchant;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.furnitur.common.api.CommonResult;
import com.furnitur.common.api.CommonPage;
import com.furnitur.core.entity.user.Merchant;
import com.furnitur.modules.admin.dto.merchant.MerchantUpdateDTO;
import com.furnitur.modules.admin.dto.auth.PasswordUpdateDTO;
import com.furnitur.modules.admin.dto.merchant.MerchantVO;
import com.furnitur.modules.admin.service.merchant.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/merchant")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;

    @GetMapping("/info")
    public CommonResult<Merchant> getInfo() {
        Merchant merchant = merchantService.getCurrentMerchantInfo();
        return CommonResult.success(merchant);
    }

    @PutMapping("/info")
    public CommonResult<Merchant> updateInfo(@Valid @RequestBody Merchant merchant) {
        merchantService.updateMerchantInfo(merchant);
        return CommonResult.success(merchant);
    }

    @PutMapping("/password")
    public CommonResult<String> updatePassword(@Valid @RequestBody PasswordUpdateDTO dto) {
        merchantService.updatePassword(dto);
        return CommonResult.success("密码修改成功");
    }

    @GetMapping("/profile")
    public CommonResult<MerchantVO> getProfile() {
        return CommonResult.success(merchantService.getMerchantProfile());
    }

    @PutMapping("/profile")
    public CommonResult<String> updateProfile(@Valid @RequestBody MerchantUpdateDTO dto) {
        merchantService.updateMerchantProfile(dto);
        return CommonResult.success("更新成功");
    }

    @PutMapping("/status/{id}")
    public CommonResult<String> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        merchantService.updateStatus(id, status);
        return CommonResult.success("状态更新成功");
    }

    @GetMapping("/list")
    public CommonResult<CommonPage<Merchant>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Merchant> page = merchantService.list(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @DeleteMapping("/{id}")
    public CommonResult<String> delete(@PathVariable Long id) {
        merchantService.delete(id);
        return CommonResult.success("删除成功");
    }
} 